package org.hhchat.leetcode.math;

/**
 * https://discuss.leetcode.com/topic/12742/o-1-space-java-solution
 * 使用双指针 / fast slow 两个指针来决定是否出现循环数
 */
public class code202_easy {

    static class Solution {
        public boolean isHappy(int n) {
            int x=n;
            int y=n;
            while(x>1){
                x = cal(x);
                y = cal(cal(y));
                if (x == 1) {
                    return true;
                }
                if (x == y) {
                    return false;
                }
            }
            return true;
        }

        static public int cal(int n){
            int x = n;
            int s = 0;
            while(x>0){
                s = s+(x%10)*(x%10);
                x = x/10;
            }
            return s ;
        }
    }

    public static void main(String[] args){

    }
}
